package teach.aistar.controller;


import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import teach.aistar.model.entity.*;
import teach.aistar.model.vo.Result;
import teach.aistar.service.*;
import teach.aistar.util.DateUtil;

import java.text.ParseException;
import java.util.Date;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private IExamineService iExamineService;

    @Autowired
    private INoticeService iNoticeService;

    @Autowired
    private ICarStyleService iCarStyleService;
    @Autowired
    private IInsureService iInsureService;

    //首页
    @GetMapping("/getNotice")
    public String index() {
        return "jsp/admin/notice";
    }

    //管理员提醒
    @GetMapping("/sendNotice")
    @ResponseBody
    public Result sendNotice(String id) throws ParseException {
        Examine examine = iExamineService.selectById(Integer.parseInt(id));
        if (examine != null) {
            Notice notice = new Notice();
            notice.setUserId(examine.getUserId());
            notice.setNoticeTime(new Date());
            notice.setNoticeStatus(0);
            notice.setNoticeType(1);
            CarStyle carStyle=iCarStyleService.findById(examine.getStId());
            notice.setNoticeText("尊敬的客户，你的车辆:"+carStyle.getStName()+"年审即将于：" + DateUtil.dateToString(examine.getExpireDate()) + "到期，请尽快续约或者联系我们!");
            iNoticeService.insert(notice);
            return new Result("200", "提醒成功", notice);
        }
        return new Result("400", "提醒失败，请稍后重试", null);
    }

    //管理员保险提醒
    @GetMapping("/sendInsureNotice")
    @ResponseBody
    public Result sendInsureNotice(String id) throws ParseException {
        Insure insure=iInsureService.selectByInsuerId(Integer.parseInt(id));
        if (insure != null) {
            Notice notice = new Notice();
            notice.setUserId(insure.getuId());
            notice.setNoticeTime(new Date());
            notice.setNoticeStatus(insure.getcId());
            notice.setNoticeType(2);
            CarStyle carStyle=iCarStyleService.findById(insure.getsId());
            notice.setNoticeText("尊敬的客户，你的车辆:"+carStyle.getStName()+"保险即将于：" + DateUtil.dateToString(insure.getdTime()) + "到期，请尽快续约或者联系我们!");
            iNoticeService.insert(notice);
            return new Result("200", "提醒成功", notice);
        }
        return new Result("400", "提醒失败，请稍后重试", null);
    }
}
